package com.caotv.wkreport.dbutils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.caotv.wkreport.dao.Bookingroom;
import com.caotv.wkreport.dao.Rooms;
import com.caotv.wkreport.dao.User;
import com.caotv.wkreport.utils.LoggingUtils;

public class BookingRoomTableTool {

	public SessionFactory sessionFactory;

	public static BookingRoomTableTool getInstance() {
		BookingRoomTableTool bookingRoomTableTool = new BookingRoomTableTool();
		bookingRoomTableTool.sessionFactory = HibernateUtil.getSessionFactory();
		return bookingRoomTableTool;
	}

	public boolean addBookingRoom(Bookingroom bookingroom) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {
			session.save(bookingroom);
			trans.commit();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when update user");
			trans.rollback();
			ex.printStackTrace();
			return false;
		}
		session.close();
		return true;
	}
	
	public List<Bookingroom> getBookingRoomInfo(int roomId){
		try {
			Session session = sessionFactory.openSession();
			List<Bookingroom> list = new ArrayList<Bookingroom>();
			Rooms room = RoomsTableTool.getInstance().getRoomByID(roomId);
			list =  session.createCriteria(Bookingroom.class)
					.add(Restrictions.eq("rooms", room))
					.list();
			session.close();
			return list;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get booking room info by roomid");
			ex.printStackTrace();
			return null;
		}
	}
	
	public List<Bookingroom> getBookingRoomInfoByDate(Date date){
		try {
			Session session = sessionFactory.openSession();
			Date startTime = (Date) date.clone();
			startTime.setHours(0);
			startTime.setMinutes(0);
			startTime.setSeconds(0);
			
			Date endTime = (Date) date.clone();
			endTime.setHours(23);
			endTime.setMinutes(59);
			endTime.setSeconds(59);
			
			List<Bookingroom> list = new ArrayList<Bookingroom>();
			list =  session.createCriteria(Bookingroom.class)
					.add(Restrictions.le("endTime", endTime))
					.add(Restrictions.ge("startTime", startTime))		
					.list();
			session.close();
			return list;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get booking room info by roomid");
			ex.printStackTrace();
			return null;
		}
	}
	
	public List<Bookingroom> getBookingRoomInfoByDateStartToEnd(Date dateS, Date dateE, int roomID){
		try {
			Session session = sessionFactory.openSession();
			Rooms room = RoomsTableTool.getInstance().getRoomByID(roomID);
			
			Date startTime = (Date) dateS.clone();
			startTime.setHours(0);
			startTime.setMinutes(0);
			startTime.setSeconds(0);
			
			Date endTime = (Date) dateE.clone();
			endTime.setHours(23);
			endTime.setMinutes(59);
			endTime.setSeconds(59);
			
			List<Bookingroom> list = new ArrayList<Bookingroom>();
			list =  session.createCriteria(Bookingroom.class)
					.add(Restrictions.le("endTime", endTime))
					.add(Restrictions.ge("startTime", startTime))
					.add(Restrictions.eq("rooms", room))
					.list();
			session.close();
			return list;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get booking room info by roomid");
			ex.printStackTrace();
			return null;
		}
	}
	
}
